<?php
require_once "model.php";
use addons\weidun_card\model\model;

class setting
{
    public $table = 'weidun_card_setting';

    public function getSetting($code)
    {
        $item = $this->getInfo($code);
        if (!empty($item)) {
            return iunserializer($item['value']);
        } else {
            return array();
        }
    }

    /**
     * 删除某设置
     * @param $code
     * @return string
     */
    public function delete($code)
    {
        global $_W;
        if (empty($code)) {
            return '';
        }
        pdo_delete($this->table, array('uniacid' => $_W['uniacid'], 'codename' => $code));
    }

    /**
     * @param $data
     * @return bool
     */
    public function update($data)
    {
        global $_W;
        $item = $this->getInfo($data['codename']);
        $data['uniacid'] = $_W['uniacid'];
        if (empty($item)) {
            return pdo_insert($this->table, $data);
        } else {
            return pdo_update($this->table, $data, array('uniacid' => $_W['uniacid'], 'codename' => $data['codename']));
        }
    }

    /**
     * @param $codename
     * @return bool
     */
    public function getInfo($codename)
    {
        global $_W;
        $sql = "SELECT * FROM " . tablename($this->table) . " WHERE uniacid = :uniacid AND codename = :codename";
        $params = array(':uniacid' => $_W['uniacid'], ':codename' => $codename);
        $item = pdo_fetch($sql, $params);
        return $item;
    }
}